import { requestData } from "../../config/requestData"
import { recordUrl } from '../../config/api.js'
let [startY, moveY, endY] = [0, 0, 0]
// pages/personal/personal.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        iconList: [
            { name: '我的消息', icon: 'icon-xiaoxi' },
            { name: '我的好友', icon: 'icon-haoyou' },
            { name: '个人主页', icon: 'icon-gerenzhuye' },
            { name: '个性装扮', icon: 'icon-gexingzhuangban' },
        ],
        navList: [
            { name: '我的音乐', url: '/pages/' },
            { name: '我的收藏', url: '/pages/' },
            { name: '我的电台', url: '/pages/' }
        ],
        recordList: [], //最近播放记录
        userInfo: {}, //用户信息,
        moveTranslate: 'transformY(0)',
        moveTransition: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) return
        this.setData({
            userInfo:JSON.parse(userInfo)
        })
        this.loadingRecordData(this.data.userInfo.userId)
    },
    // 方法
    goLogin () {
        wx.reLaunch({
          url: '/pages/login/login',
        })
    },
    handleUserInfo () {
        wx.getUserInfo({
            success: (res) => {
                // const { nickName, avatarUrl } = res.userInfo
                this.setData({
                    userInfo: res.userInfo
                })
                wx.setStorageSync('userInfo', JSON.stringify(this.data.userInfo))
                this.loadingRecordData()
            }
        })
    },
    handleTouchStart(e) {
        this.setData({
            moveTransition: ''
        })
        startY = e.touches[0].clientY
        // startY, moveY, endY
    },
    handleTouchMove (e) {
        endY = e.touches[0].clientY
        moveY = endY - startY
        if (moveY <= 0) return
        if (moveY >= 150) return moveY = 150
        this.setData({
            moveTranslate: `translateY(${moveY}rpx)`
        })
    },
    handleTouchEnd () {
        [startY, moveY, endY] = [0]
        this.setData({
            moveTranslate: 'translateY(0)',
            moveTransition: 'transition: transform 1s linear'
        })
    },
    // 加载方式
    async loadingRecordData(uid){
        const options = {
            url: recordUrl,
            data: {
                uid,
                type: 1
            }
        }
        const res = await requestData(options)
        const { code, weekData } = res
        if (code === 200) {
            this.setData({
                recordList: weekData
            })
        }
    },
    loadingUserInfo () {
        this.setData({
            userInfo: JSON.parse(wx.getStorageSync('userInfo'))
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})